<template>
        <el-dialog :title="title" size="large" v-model="editFormVisible" :close-on-click-modal="false">
            <el-form ref="form" :model="form" :rules="rules" label-width="110px">
                <el-form-item label="仓库" placeholder="请选择"  prop="FStonehouseID">
                    <sy-select v-model="form.FStonehouseID"  valueKey="FID"
                               optionData="server/setting/Stonehouse.ashx?cmd=GetOptions">
                    </sy-select>
                </el-form-item>
                <el-form-item label="备注">
                    <el-input type="textarea" v-model="form.FNote"></el-input>
                </el-form-item>
            </el-form>
            <el-table
                    :data="listdata"
                    height="250"
                    border
                    v-loading="listLoading"
                    style="width: 100%">
                <el-table-column
                        prop="FProductID"
                        label="产品"
                        width="250">
                    <template scope="scope">
                        <sy-select v-model="scope.row.FProductID" valueKey="FID" @change="changeproduct" :extendData="scope.row"
                                   :optionData="'server/setting/Product.ashx?cmd=GetOptions&where='+productwhere">
                        </sy-select>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="ProductModel"
                        label="规格"
                        width="200">
                </el-table-column>
                <el-table-column
                        prop="ProductUnitName"
                        label="单位"
                        width="200">
                </el-table-column>
                <el-table-column
                        prop="FQuantity"
                        label="数量"
                        width="180">
                    <template scope="scope">
                        <el-input type="number"  v-model="scope.row.FQuantity"></el-input>
                    </template>
                </el-table-column>
                    <el-table-column
                            prop=""
                            label="操作">
                        <template scope="scope">
                         <a v-if="form.FStatus==0" href="javascript:;" @click="addRow"><i style="color:#8391a5" class="el-icon-plus"></i></a>
                        <a v-if="form.FStatus==0" href="javascript:;" @click="delRow(scope.$index)"><i style="color:#8391a5" class="el-icon-delete"></i></a>
                        </template>
                </el-table-column>
            </el-table>

            <div slot="footer" class="dialog-footer">
                <el-button @click.native="editFormVisible = false">取 消</el-button>
                <el-button type="primary" v-if="showSave" @click="save" :loading="loading">保存</el-button>
            </div>
      </el-dialog>
</template>

<script>
    import util from '../../common/util'
    import mixin from '../../common/mixin'
    export default {
        data() {
            return {
                form: {},
                showSave:true,
                rules: {
                   // FName: [{type: "date",name:"FCreateDate",text:"跟进时间", value: null}]
                },
                loading:false,
                editFormVisible:false,
                title:'',
                listdata: [],
                listLoading:false
            }
        },
        computed: {
            productwhere: function () {
                let result=[]
               for(let pro of this.listdata){
                    if(pro.FProductID) {
                        result.push(pro.FProductID)
                    }
               }
               if(result.length==0)
               {
                   return ""
               }
               return ','+result.join(',')+','
            }
        },
        methods: {
            switchLoading(flag){
                this.loading = flag;
            },
            _openEdit(record) {
                this.editFormVisible = true;
                this.form = Object.assign({},record);

                if(this.form.FStatus==0)
                {
                  this.title = '编辑'
                }
                else{
                  this.title = '查看'
                  this.showSave=false;
                }

                util.get("server/business/StonehouseFlowDetail.ashx?cmd=GetList",
                    {where:"FStonehouseflowID="+this.form.FID},this.switchLoading,result=>{
                        this.listdata = result.listdata
                    })
            },
            _openAdd(record) {
                this.editFormVisible = true;
                this.form = {FID:0,FType:1,FAddTime:new Date(),FUserID:global.user.FID,listdata:[],FStatus:0};
                this.title = '新增'
                this.listdata.push({FProductID: '',ProductModel:'',Quantity: ''})
            },
            save:function () {
                if(!this.form.FStonehouseID)
                {
                    util.msgWarning("请选择仓库");
                    return;
                }
                if(!this.listdata || this.listdata.length==0) {
                    util.msgWarning("请选择要入库的货物");
                    return;
                }
                for(let item of this.listdata)
                {
                    if(isNaN(item.FQuantity))
                    {
                        util.msgWarning("请填写正确的数量");
                        return;
                    }
                }
                this.form.listdata = JSON.stringify(this.listdata);
                util.post("server/business/StonehouseFlow.ashx?cmd=Save",this.form,this.switchLoading,result=> {
                    util.msgSuc("保存成功")
                    this.editFormVisible = false
                    this.$emit("onSaveSuc",this.form)
                })
            },
            changeproduct(val,record,row){
                 row.ProductModel = record.FModel
                 row.ProductUnitName = record.UnitName
            },
            addRow(){
                this.listdata.push({FProductID: '',ProductModel:'',Quantity: ''})
            },
            delRow(index){
                this.listdata.splice(index,1);
                if(this.listdata.length==0)
                {
                    this.listdata.push({FProductID: '',ProductModel:'',Quantity: ''})
                }
            }
        },
        mounted() {

        }
    }
</script>
